/*
 * Camera.cpp
 *
 *  Created on: Mar 30, 2011
 *      Author: s88900
 */

#include "Camera.h"

Camera::Camera(int i, Generator* g, Merger* m) :
	Thread("Camera") {
	image = 0;
	number = i;
	world = g;
	merger = m;
}

void Camera::run() {
	for (;;) {
		refresh();
		sendImage();
		int sleeptime = (rand() % 100 + 1) * 666.67 * 2;
		//cout << "sleep: " << sleeptime << endl;
		usleep(sleeptime); // +/- 15 fps
	}
}

void Camera::refresh() {
	image = world->getImage(number);
	//cout << "camera " << number << " got image: " << image << endl;
}

void Camera::sendImage() {
	merger->newImage(number, image);
}

Camera::~Camera() {
	image = 0;
	number = 0;
	delete world;
	delete merger;
}
